<template>
  <div class="div-main">
    <div class="div-login">
      <el-form class="form-login" ref="userVo" :model="userVo">

        <el-input v-model="userVo.loginacct" class="input-username" placeholder="用户名/手机号"></el-input>
        <div>
          <span></span>
        </div>

        <el-input v-model="userVo.password" class="input-pswd" type="password" placeholder="用户密码">
        </el-input>

        <el-button round class="btn-login" @click="login">登录</el-button>

        <div class="tipareas">
          <a style="margin-right: 5px">忘记密码?</a>
          <span>|</span>
          <a href="#" style="margin-left: 5px; margin-right: 5px; color: white" @click="register">注册</a>
        </div>
      </el-form>

      <div class="third-party">
        <h4 style="margin-bottom: 10px">第三方登录</h4>
        <a @click="openWeiboLogin">
          <i class="iconfont" style="font-size: 45px; margin-right: 10px;">&#xe62f;</i>
        </a>
        <i class="iconfont" style="font-size: 45px; margin-right: 10px;">&#xe636;</i>
        <i class="iconfont" style="font-size: 45px;">&#xe6b4;</i>
      </div>

    </div>
  </div>
</template>

<script>
import member from '@/api/member/member'
import {mapActions, mapGetters} from "vuex";
import {setToken} from "@/utils/auth";

export default {
  name: "login",
  data() {
    return {
      userVo: {}
    }
  },

  methods: {
    ...mapActions(["setUser"]),

    openWeiboLogin() {
      location.href = "https://api.weibo.com/oauth2/authorize?client_id=477905441&response_type=code&redirect_uri=http://gmmallauth.com/weibo/login"
    },

    //登录
    login() {
      member.memberLogin(this.userVo)
          .then(res => {
            //存储用户登录信息到本地
            let user = JSON.stringify(res.data);
            localStorage.setItem("gm-mall-user", user);
            //存储token到cookie
            setToken(res.data.token)
            //存储用户信息到vuex
            this.setUser(res.data)
            //保存用户的心愿单
            //跳转页面
            this.$router.push('/')
          }).catch(error => {
            this.$message({
              type: 'error',
              message: '登录失败'
            })
      })
    },

    //注册
    register() {
      this.$router.push('/register')
    }
  }
}
</script>

<style scoped>
.div-main {
  width: 100%;
  height: 100%;
}

.div-login {
  background-color: rgba(61, 61, 61, 0.5);
  border-radius: 10px;
  float: right;
  height: 25rem;
  width: 23rem;
  /*padding-right: 10rem;*/
  margin-right: 10rem;
  margin-top: 5rem;
  margin-bottom: 4rem;
}

.form-login {
  margin-top: 2rem;
  margin-right: 5px;
  margin-left: 5px;
}

.input-username {
  background-color: #99a9bf;
  margin-bottom: 10px;
  border-radius: 5px;
}

.input-pswd {
  margin-bottom: 5px;
}

.btn-login {
  margin-top: 10px;
  width: 100%;
}

.tipareas {
  margin-top: 10px;
  text-align: right;
  font-size: 15px;
  color: #ffffff;
}

.third-party {
  margin-top: 6rem;
  text-align: center;
}
</style>